package com.ruoyi.system.service.waterService.waterServiceImpl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.waterEnity.*;
import com.ruoyi.system.mapper.waterMapper.ProductMapper;
import com.ruoyi.system.mapper.waterMapper.UserMapper;
import com.ruoyi.system.service.waterService.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<UserInfoBO> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public List<ProductInfoBO> selectAllProduct() {
        return productMapper.selectAllProduct();
    }

    @Override
    public List<ProductInfoBO> selectAllNorm(int id) {
        return productMapper.selectAllNorm(id);
    }

    @Override
    public ProductInfoBO selectChildProductById(int id) {
        return productMapper.selectChildProductById(id);
    }

    /**
     * 添加商品
     *
     * @param productInfoBO
     * @return
     */
    @Override
    public int insertProduct(ProductInfoBO productInfoBO) {
        return productMapper.insertProduct(productInfoBO);
    }

    @Override
    public int insertNorm(ProductInfoBO productInfoBO) {
        return productMapper.insertNorm(productInfoBO);
    }

    @Override
    public String checkProduct_name(ProductInfoBO productInfoBO) {
        return productMapper.checkProduct_name(productInfoBO);
    }

    /**
     * 添加商品分类
     *
     * @param productInfoBO
     * @return
     */
    @Override
    public int insertSort(ProductInfoBO productInfoBO) {
        return productMapper.insertSort(productInfoBO);
    }

    /**
     * 上架，下架商品
     *
     * @param
     * @return
     */
    @Override
    public int updateProductState(ProductInfoBO productInfoBO) {
        return productMapper.updateProductState(productInfoBO);
    }

    /**
     * 修改商品
     *
     * @param productInfoBO
     * @return
     */
    @Override
    public int updateProduct(ProductInfoBO productInfoBO) {
        return productMapper.updateProduct(productInfoBO);
    }

    @Override
    public int updateNorm(ProductInfoBO productInfoBO) {
        return productMapper.updateNorm(productInfoBO);
    }

    /**
     * 根据商品id查询
     *
     * @param id
     * @return
     */
    @Override
    public  List<ProductInfoBO> selectProductById(String id) {
        return productMapper.selectProductById(id);
    }

    /**
     * 根据商品名称查询
     *
     * @param product_name
     * @return
     */
    @Override
    public List<ProductInfoBO> selectProductByProduct_name(String product_name) {
        return productMapper.selectProductByProduct_name(product_name);
    }
    /**
     * 根据商品状态查询
     *
     * @param state
     * @return
     */
    @Override
    public List<ProductInfoBO> selectProductByState(String state) {
        return productMapper.selectProductByState(state);
    }


    /**
     * 送货员身份设置
     *
     * @param identify
     * @param id
     * @return
     */
    @Override
    public int updateUserByIdentify(String identify, int id) {
        return productMapper.updateUserByIdentify(identify, id);
    }

    /**
     * 查看全部配送信息
     *
     * @return
     */
    @Override
    public List<DistributionEntity> selectDistributionList() {
        return productMapper.selectDistributionList();
    }

    /**
     * 修改配送信息
     *
     * @param distributionEnity
     * @return
     */
    @Override
    public int updateDistribution(DistributionEntity distributionEnity) {
        return productMapper.updateDistribution(distributionEnity);
    }


    /**
     * 查询全部分类
     *
     * @return
     */
    @Override
    public List<SortEntity> selectSortList() {
        return productMapper.selectSortList();
    }

    /**
     * 删除主商品和他的子商品
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteProductAndNorm(String ids) {
        Long[] productids = Convert.toLongArray(ids);
        int productNum = productMapper.deleteProduct(productids);
        if (productNum > 0) {
            int normNum = productMapper.deleteNormByProduct_id(productids);
            if (normNum >= 0) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    @Override
    public int updateProductStock(ProductInfoBO productInfoBO) {
        return productMapper.updateProductStock(productInfoBO);
    }

    @Override
    public int deleteChildProduct(String ids) {
        Long[] productids = Convert.toLongArray(ids);
        int num = productMapper.deleteChildProduct(productids);
            if (num >= 0) {
                return 1;
            } else {
                return 0;
            }
        }

    @Override
    public List<ProductInfoBO> selectAllProductByCondition(String product_id, String product_name, String state,String sort) {
       String flag= product_id+ product_name + state + sort ;
       List<ProductInfoBO> list = null;
       if (StringUtils.isEmpty(flag)){
           list = productMapper.selectAllProduct();
       }else if (StringUtils.isNotEmpty(product_id)){
           list = productMapper.selectProductById(product_id);
       }else if (StringUtils.isNotEmpty(product_name)){
           list = productMapper.selectProductByProduct_name(product_name);
       }else if (StringUtils.isNotEmpty(state)){
           list = productMapper.selectProductByState(state);
       }else if(StringUtils.isNotEmpty(sort)){
           list = productMapper.selectProductBySort(sort);
       }
           else{
           list = productMapper.selectAllProduct();
       }
        return list;
    }

    @Override
    public SortEntity selectSortBySort_id(int sort_id) {
        return productMapper.selectSortBySort_id(sort_id);
    }
    /**
     * 修改主商品下全部商品状态
     */
    @Override
    public int updateProductState1(ProductInfoBO productInfoBO) {
        return productMapper.updateProductState1(productInfoBO);
    }

    @Override
    public ProductInfoBO checkProductState(ProductInfoBO productInfoBO) {
        return  productMapper.checkProductState(productInfoBO);
    }
    /**
     * 检查子商品的状态
     */
    @Override
    public  List<ProductInfoBO> checkChildProductState(ProductInfoBO productInfoBO) {
        return productMapper.checkChildProductState(productInfoBO);
    }

    @Override
    public int productImage(ImageEntity imageEntity) {
        return productMapper.productImage(imageEntity);
    }


}
